package com.example.administrator.shandon2020_1;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.shandon2020_1.util.SaveandGet;
import com.nle.mylibrary.forUse.mdbus4150.MdBus4150RelayListener;
import com.nle.mylibrary.forUse.mdbus4150.Modbus4150;
import com.nle.mylibrary.transfer.DataBusFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import cn.com.newland.nle_sdk.requestEntity.SignIn;
import cn.com.newland.nle_sdk.responseEntity.SensorInfo;
import cn.com.newland.nle_sdk.responseEntity.User;
import cn.com.newland.nle_sdk.responseEntity.base.BaseResponseEntity;
import cn.com.newland.nle_sdk.util.NCallBack;
import cn.com.newland.nle_sdk.util.NetWorkBusiness;
import retrofit2.Call;

public class MainActivity extends AppCompatActivity {
    private Button btn_setting;
    private TextView tv_temp,tv_fan,textView3,textView4;
    private NetWorkBusiness netWorkBusiness;
    private String acc,ip,username,password,id,fan_Do,fan_id,temp_id;
    private Integer rate;
    private Modbus4150 modbus4150;
    private Integer a=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        acc=SaveandGet.getacc(MainActivity.this);
        if(ip!=null){
            netWorkBusiness=new NetWorkBusiness(acc,ip);
            Mythread mythread=new Mythread();
            mythread.start();
        }
        btn_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,setting.class);
                startActivity(intent);
            }
        });
    }
    private void init(){
        btn_setting=findViewById(R.id.btn_setting);
        tv_temp=findViewById(R.id.tv_temp);
        tv_fan=findViewById(R.id.tv_fan);
        textView3=findViewById(R.id.textView3);
        textView3=findViewById(R.id.textView4);
        modbus4150=new Modbus4150(DataBusFactory.newSocketDataBus("172.21.2.201",4196));
        Map<String,String> map=new HashMap<>();
        if (map != null&map.get("ip")!=null) {
            ip=map.get("ip");
            username=map.get("username");
            password=map.get("password");
            id=map.get("id");
            ip=map.get("ip");
            fan_Do=map.get("fan_Do");
            fan_id=map.get("fan_id");
            temp_id=map.get("temp_id");
            rate=Integer.parseInt(map.get("rate"));
            netWorkBusiness=new NetWorkBusiness("",ip);
            netWorkBusiness.signIn(new SignIn(username, password), new NCallBack<BaseResponseEntity<User>>(this) {
                @Override
                protected void onResponse(BaseResponseEntity<User> response) {
                    if(response!=null){
                        if(response.getStatus()==0){
                            SaveandGet.saveacc(response.getResultObj().getAccessToken(),MainActivity.this);
                        }
                    }
                }

                @Override
                public void onFailure(Call<BaseResponseEntity<User>> call, Throwable t) {
                    super.onFailure(call, t);
                    Toast.makeText(MainActivity.this,t.getMessage(),Toast.LENGTH_SHORT).show();
                }
            });
        }


    }
    private class  Mythread extends Thread{
        @Override
        public void run() {
            super.run();
            while (true){
                try {
                    Thread.sleep(rate);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                netWorkBusiness.getSensor(id, fan_id, new NCallBack<BaseResponseEntity<SensorInfo>>(MainActivity.this) {
                    @Override
                    protected void onResponse(BaseResponseEntity<SensorInfo> response) {
                        if(response!=null){
                            String value = response.getResultObj().getValue();
                            double v = Double.parseDouble(value);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    tv_temp.setText(value);
                                }
                            });
                            if (v>30){
                                if(a==0){
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Date date = new Date();
                                            SimpleDateFormat sp=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                                            sp.format(date);
                                            textView3.append(date+"\n");
                                            textView4.append("启动\n");
                                            a=1;
                                        }
                                    });
                                }
                                try {
                                    modbus4150.openRelay(1, new MdBus4150RelayListener() {
                                        @Override
                                        public void onCtrl(boolean isSuccess) {
                                            Log.i("main",isSuccess+"");
                                        }
                                    });
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            else {
                                if(a==1){
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Date date = new Date();
                                            SimpleDateFormat sp=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                                            sp.format(date);
                                            textView3.append(date+"\n");
                                            textView4.append("关闭\n");
                                            a=0;
                                        }
                                    });
                                }
                                try {
                                    modbus4150.closeRelay(1, new MdBus4150RelayListener() {
                                        @Override
                                        public void onCtrl(boolean isSuccess) {
                                            Log.i("main",isSuccess+"");
                                        }
                                    });
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                });
            }
        }
    }
}
